import os
import operator
import csv
  
#读取csv文件
num = 0
cnt = 0
flag = 0
for i in range(400):
    filename = './cluster/' + str(i) + '.csv'
    #filename = 'test.csv'
    filename2 = './trajectory/' + "ID_"+str(i) + '.csv'
    if not os.path.exists(filename):
        continue
    else:
        print(i)
        with open(filename, "r") as f:
                reader = csv.reader(f)
                trajectories = []
                trajectory = []
                prev = (0,0)
                for row in reader:
                    gps = (float(row[1]), float(row[2]))
                    if operator.eq(prev, gps):
                        if flag:
                            continue
                        cnt = cnt+1                        
                        if cnt >= 9:
                            flag = 1
                            trajectories.append(trajectory)
                            trajectory = [prev]
                        continue
                    else:
                        flag = 0
                        cnt = 0
                        trajectory.append(gps)
                    prev = gps
        if len(trajectory) > 1:
            trajectories.append(trajectory)
        
        print(len(trajectories))

        with open(filename2, 'w', newline='') as csvfile:
            spamwriter = csv.writer(csvfile)
            spamwriter.writerows(trajectories)
                    
